package com.shimh.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson.support.spring.FastJsonViewResponseBodyAdvice;
import com.shimh.common.interceptor.ClearTokenInteceptor;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{
	
	
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
    	
        registry.addInterceptor(clearTokenInteceptor()).addPathPatterns("/**");
    }
	
    @Bean
    public ClearTokenInteceptor clearTokenInteceptor(){
    	ClearTokenInteceptor clearTokenInteceptor = new ClearTokenInteceptor();
    	return clearTokenInteceptor;
    }
    
    
	@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        FastJsonHttpMessageConverter  fastConverter = new FastJsonHttpMessageConverter ();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.DisableCircularReferenceDetect
        );

        
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        
        fastConverter.setFastJsonConfig(fastJsonConfig);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);

        converters.add(fastConverter);
    }
	
	
	@Bean
	public FastJsonViewResponseBodyAdvice FastJsonViewResponseBodyAdvice(){
		FastJsonViewResponseBodyAdvice advice = new FastJsonViewResponseBodyAdvice();
		return advice;
	}
}
